<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>transform案例-实现任意元素居中效果</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 400px;
            height: 400px;
            border-radius: 50%;
            background-color: #ccc;
            margin: 100px auto;
            position: relative;
        }

        .rec {
            width: 100px;
            height: 100px;
            background-color: #f00;
            position: absolute;
            /* 定位的百分比参照父容器的宽高 */
            left: 50%;
            top: 50%;
            /* 使用 transform 实现元素居中，百分比是参照元素本身的宽高 */
            transform: translate(-50%, -50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="rec"></div>
    </div>
</body>
</html>